package com.fengze.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fengze.common.annotation.Log;
import com.fengze.common.core.controller.BaseController;
import com.fengze.common.core.domain.AjaxResult;
import com.fengze.common.enums.BusinessType;
import com.fengze.business.domain.FzProductsCategory;
import com.fengze.business.service.IFzProductsCategoryService;
import com.fengze.common.utils.poi.ExcelUtil;
import com.fengze.common.core.page.TableDataInfo;

/**
 * 产品类别Controller
 * 
 * @author ruoyi
 * @date 2024-01-26
 */
@RestController
@RequestMapping("/products/category")
public class FzProductsCategoryController extends BaseController
{
    @Autowired
    private IFzProductsCategoryService fzProductsCategoryService;

    /**
     * 查询产品类别列表
     */
    @PreAuthorize("@ss.hasPermi('products:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(FzProductsCategory fzProductsCategory)
    {
        startPage();
        List<FzProductsCategory> list = fzProductsCategoryService.selectFzProductsCategoryList(fzProductsCategory);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('products:category:list')")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(FzProductsCategory category){
        List<FzProductsCategory> categorys = fzProductsCategoryService.selectCategoryList(category);
        return AjaxResult.success("查询数据成功",fzProductsCategoryService.buildTreeSelect(categorys));
    }

    /**
     * 导出产品类别列表
     */
    @PreAuthorize("@ss.hasPermi('products:category:export')")
    @Log(title = "产品类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FzProductsCategory fzProductsCategory)
    {
        List<FzProductsCategory> list = fzProductsCategoryService.selectFzProductsCategoryList(fzProductsCategory);
        ExcelUtil<FzProductsCategory> util = new ExcelUtil<FzProductsCategory>(FzProductsCategory.class);
        util.exportExcel(response, list, "产品类别数据");
    }

    /**
     * 获取产品类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('products:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fzProductsCategoryService.selectFzProductsCategoryById(id));
    }

    /**
     * 新增产品类别
     */
    @PreAuthorize("@ss.hasPermi('products:category:add')")
    @Log(title = "产品类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FzProductsCategory fzProductsCategory)
    {
        return toAjax(fzProductsCategoryService.insertFzProductsCategory(fzProductsCategory));
    }


    /**
     * 修改产品类别
     */
    @PreAuthorize("@ss.hasPermi('products:category:edit')")
    @Log(title = "产品类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FzProductsCategory fzProductsCategory)
    {
        return toAjax(fzProductsCategoryService.updateFzProductsCategory(fzProductsCategory));
    }

    /**
     * 删除产品类别
     */
    @PreAuthorize("@ss.hasPermi('products:category:remove')")
    @Log(title = "产品类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int deleteCount = fzProductsCategoryService.deleteFzProductsCategoryByIds(ids);
        if(deleteCount == -1){
            return AjaxResult.error("删除失败，请先删除其子类后再删除");
        }

        return toAjax(deleteCount);
    }
}
